package myJs.implicits

import cats.effect._
import cats.effect.unsafe.implicits.global
import myJs.Utils.layer
import myJs.tool.Tool
import org.scalajs.dom.{MessageEvent, WebSocket, XMLHttpRequest}

import scala.scalajs.js

trait MyIOTool {

  implicit class MyIO[A](v: IO[A]) {

    def flatMapSync[B](f: A => SyncIO[B]) = {
      v.flatMap(a => f(a).to[IO])
    }

    def >>*[B](f: SyncIO[B]) = {
      flatMapSync(_ => f)
    }

    def showLoading(showLoading: Boolean = true) = {
      if (showLoading) {
        IO { layer.alert(Tool.myElement, Tool.layerOptions) }.flatMap { index =>
          v
            .map { x =>
              layer.close(index)
            }
        }
      } else {
        v
      }
    }.void

  }

}
